﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Transform;
using ScheduleMaker.DB.Domain.Core;

namespace ScheduleMaker.DB
{
    public class EmergencyScheduleDAOSchool : AbstractDAO<EmergencyScheduleSchool>
    {

        public IList<EmergencyScheduleSchool> getEmergencyScheduleByDate(DateTime date)
        {
            createSession();
            ICriteria criteria = session.CreateCriteria(typeof(EmergencyScheduleSchool))
                    .Add(Restrictions.Eq("Date", date.Ticks));
            IList<EmergencyScheduleSchool> emergencySchedule = criteria.List<EmergencyScheduleSchool>();
            closeSession();
            return emergencySchedule;
        }

        public IList<EmergencyScheduleSchool> getByClassroom(Classroom classroom)
        {
            createSession();
            ICriteria criteria = session.CreateCriteria(typeof(EmergencyScheduleSchool))
                .Add(Restrictions.Eq("Classroom", classroom)).SetResultTransformer(new DistinctRootEntityResultTransformer());
            IList<EmergencyScheduleSchool> schedule = criteria.List<EmergencyScheduleSchool>();
            closeSession();
            return schedule;
        }

        public IList<EmergencyScheduleSchool> getByGroup(Group group)
        {
            createSession();
            ICriteria criteria = session.CreateCriteria(typeof(EmergencyScheduleSchool))
                .Add(Restrictions.Eq("Group", group)).SetResultTransformer(new DistinctRootEntityResultTransformer());
            IList<EmergencyScheduleSchool> schedule = criteria.List<EmergencyScheduleSchool>();
            closeSession();
            return schedule;
        }

        public IList<EmergencyScheduleSchool> getByTeacher(Teacher teacher)
        {
            createSession();
            ICriteria criteria = session.CreateCriteria(typeof(EmergencyScheduleSchool))
                .Add(Restrictions.Eq("Teacher", teacher)).SetResultTransformer(new DistinctRootEntityResultTransformer());
            IList<EmergencyScheduleSchool> schedule = criteria.List<EmergencyScheduleSchool>();
            closeSession();
            return schedule;
        }

        public void deleteAll()
        {
            createTransaction();
            session.Delete("from EmergencyScheduleSchool");
            session.Flush();
        }

    }
}
